package com.xyoto.community.service.impl;

import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.constant.OperationStatus;
import com.xyoto.community.constant.UserLoginStatus;
import com.xyoto.community.dto.SaveQuestionCommentReplyDTO;
import com.xyoto.community.entity.comment.QuestionComment;
import com.xyoto.community.entity.comment.QuestionCommentReply;
import com.xyoto.community.entity.main.Question;
import com.xyoto.community.enums.QuestionsEnum;
import com.xyoto.community.enums.WechatLoginEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.comment.QuestionCommentMapper;
import com.xyoto.community.mapper.comment.QuestionCommentReplyMapper;
import com.xyoto.community.mapper.main.MessageMapper;
import com.xyoto.community.mapper.main.QuestionMapper;
import com.xyoto.community.po.QuestionCommentReplyPo;
import com.xyoto.community.service.QuestionCommentReplyService;
import com.xyoto.community.utils.JwtUtils;
import com.xyoto.community.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xyoto.community.service.MessageService;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:   问答评论回复service接口实现类
 * Author:      挽风
 * Date:        2020
 * Copyright:   2020 by 挽风1.0版本
 * Description:
 **/

//@Service
@Slf4j
public class QuestionCommentReplyServiceImpl implements QuestionCommentReplyService {

    @Resource
    private QuestionCommentReplyMapper questionCommentReplyMapper;

    @Resource
    private QuestionCommentReplyMapper replyMapper;

    @Resource
    private HttpServletRequest request;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private QuestionCommentMapper questionCommentMapper;

    @Resource
    private MessageMapper messageMapper;

    @Autowired
    private MessageService messageService;


    /**
     * 根据问答评论id查询父级评论点赞数
     *
     * @param id 问答id
     *
     * @return 点赞数
     */
    @Override
    public List<QuestionCommentReplyPo> getQuestionCommentReply(Integer id) {
        return questionCommentReplyMapper.getQuestionCommentReplyList(id);
    }

    /**
     * 新增问答评论回复
     * @param replyDTO
     * @return
     */
    @Override
    public Result saveQuestionCommentReply(SaveQuestionCommentReplyDTO replyDTO) {

        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        if (userId == UserLoginStatus.UN_LOGIN) {ExceptionUtils.error(WechatLoginEnum.USER_NOT_LOGIN);};

        log.info("开始执行评论回复判断");
        //判断是否有该评论
        QuestionComment getQuestionComment = questionCommentMapper.getQuestionCommentById(replyDTO.getQuestionCommentId());
        if (getQuestionComment == null || getQuestionComment.getDeleteStatus() == DeleteStatus.YES) {
            ExceptionUtils.error(QuestionsEnum.QUESTION_COMMENTS_NOT_EXIST);
        }

        //判断是否存在该问答
        Integer questionId = getQuestionComment.getQuestionId();
        Question question = questionMapper.getQuestionById(questionId);
        if (question == null || question.getDeleteStatus() == DeleteStatus.YES){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_NULL);
        }

        log.info("开始执行新增问答评论回复功能");
        //有该评论就执行新增回复功能
        QuestionCommentReply reply = new QuestionCommentReply();
        BeanUtils.copyProperties(replyDTO,reply);
        reply.setUserId(userId);
        reply.setDeleteStatus(DeleteStatus.NO);

        log.info("开始执行SQL");
        Integer saveReply = replyMapper.saveQuestionCommentReply(reply);
        if (saveReply == null || saveReply == 0){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_REPLY_FAILED);
        }
//        //添加消息
//        messageService.saveMessage(userId, getQuestionComment.getUserId(), reply.getId(),
//                ModuleType.QUESTION_COMMENT_REPLY,questionId);

        return Result.success();
    }

    /**
     * 删除问答评论回复
     *
     * @param questionCommentReplyId 回复id
     * @return
     */
    @Override
    @Transactional
    public Result removeQuestionCommentReply(Integer questionCommentReplyId) {
        if (null == questionCommentReplyId || questionCommentReplyId <= 0) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_ID_ERROR);
        }
        //todo 检验权限 管理员权限未加
        //此条问答主人、此问答评论主人、此问答评论回复主人以及管理员可删此回复，其余用户不可删
        //获取回复主人id
        QuestionCommentReply questionCommentReply = questionCommentReplyMapper
                .getQuestionCommentReplyById(questionCommentReplyId);
        checkQuestionCommentReplyExist(questionCommentReply);
        Integer replyUserId = questionCommentReply.getUserId();
        //获取评论主人id
        QuestionComment questionComment = questionCommentMapper
                .getQuestionCommentById(questionCommentReply.getQuestionCommentId());
        checkQuestionCommentExist(questionComment);
        Integer questionCommentUserId = questionComment.getUserId();
        //获取此问答主人id
        Question question = questionMapper.getQuestionById(questionComment.getQuestionId());
        checkQuestionExist(question);
        Integer questionUserId = question.getUserId();
        //当前用户id
        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        if (userId.equals(questionUserId)
                || userId.equals(questionCommentUserId)
                || userId.equals(replyUserId)) {
            //业务操作
            QuestionCommentReply newQuestionCommentReply = new QuestionCommentReply();
            newQuestionCommentReply.setDeleteStatus(DeleteStatus.YES);
            newQuestionCommentReply.setId(questionCommentReplyId);
            newQuestionCommentReply.setUserId(userId);
            Integer state = questionCommentReplyMapper
                    .removeQuestionCommentReply(newQuestionCommentReply);
            if (state != OperationStatus.SUCCESS) {
                ExceptionUtils.error(QuestionsEnum.DELETE_QUESTION_COMMENT_REPLY_FAILED);
            }
//            Message message = new Message();
//            message.setType(ModuleType.QUESTION_COMMENT_REPLY);
//            message.setModuleId(questionCommentReplyId);
//            message.setThirdParentId(question.getId());
//            Integer newState = messageMapper.removeMessage(message);
//            if (newState != OperationStatus.SUCCESS) {
//                ExceptionUtils.error(MessageEnum.DELETED_QUESTION_COMMENT_REPLY_MESSAGE_FAILED);
//            }
//            return Result.success();
        } else {
            ExceptionUtils.error(QuestionsEnum.NOT_POWER);
        }
        return Result.success();
    }

    /**
     * 检查问答是否存在或已经被逻辑删除
     *
     * @param question
     */
    private void checkQuestionExist(Question question) {
        if (null == question) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_NULL);
        }
        if (question.getDeleteStatus().equals(DeleteStatus.YES)) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_DELETE);
        }
    }

    /**
     * 检查问答评论是否存在或已经被逻辑删除
     *
     * @param questionComment
     */
    private void checkQuestionCommentExist(QuestionComment questionComment) {
        if (null == questionComment) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_NOT_EXISTED);
        }
        if (questionComment.getDeleteStatus().equals(DeleteStatus.YES)) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_IS_DELETE);
        }
    }

    /**
     * 检查问答评论回复是否存在或已经被逻辑删除
     *
     * @param questionCommentReply
     */
    private void checkQuestionCommentReplyExist(QuestionCommentReply questionCommentReply) {
        if (null == questionCommentReply) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_NOT_EXISTED);
        }
        if (questionCommentReply.getDeleteStatus().equals(DeleteStatus.YES)) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_REPLY_IS_DELETE);
        }
    }
}
